package Volume1.ch6;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Employee排序——Comparable接口测试
 * @Auther jacob
 * @Date 2020/8/8 00:11
 */
public class EmployeeSortTest {

    public static void main(String[] args) {
        Employee[] staff = new Employee[3];

        staff[0] = new Employee("aaa", 40.0);
        staff[1] = new Employee("bbb", 20.0);
        staff[2] = new Employee("ccc", 30.0);

        // 通过实体类中的Comparable接口排序
//        Arrays.sort(staff);
        // 通过自定义的OtherComparator排序
//        Arrays.sort(staff, new OtherComparator());
        // 通过使用lambda表达式实现排序
        Arrays.sort(staff, ((o1, o2) -> {
            return o2.getSalary() < o1.getSalary() ? -1 : o2.getSalary() > o1.getSalary() ? 1 : 0;
        }));

        for (Employee employee : staff) {
            System.out.println("name = " + employee.getName() + ", salary = " + employee.getSalary());
        }
    }
}

class OtherComparator implements Comparator<Employee>{


    @Override
    public int compare(Employee o1, Employee o2) {
        return o2.getSalary() < o1.getSalary() ? -1 : o2.getSalary() > o1.getSalary() ? 1 : 0;
    }
}
